<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>常用联系人</title>
    <!--引入vue.js库-->
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script src="/js/vue.js"></script>
    <!-- element-ui引入样式 展示数据的-->
    <link rel="stylesheet" href="/css/index.css">
    <!-- element-ui引入组件库 -->
    <script src="/js/index.js"></script>
    <!--引入axios库  从后端获取数据-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <!--搜索-->
    <el-row>
        <!--一行分为4列-->
        <el-col :span="12">
            <el-input v-model="searchForm.name" size="mini" placeholder="请输入关键字"></el-input>
        </el-col>
        <el-col :span="2">
            &nbsp;&nbsp;
            <el-button type="success" icon="el-icon-search" size="mini" @click="search">搜索</el-button>&nbsp;
        </el-col>
        <el-col :span="2">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button>
        </el-col>
        <el-col :span="8">&nbsp;</el-col>
    </el-row>
    <!--table 数据-->
    <el-table
            :data="tableData"
            style="width: 100%">
        <!--        <el-table-column-->
        <!--                prop="id"-->
        <!--                label="id"-->
        <!--                width="200">-->
        <!--        </el-table-column>-->
        <!--        <el-table-column-->
        <!--                prop="userid"-->
        <!--                label="用户id"-->
        <!--                width="200">-->
        <!--        </el-table-column>-->
        <el-table-column
                prop="name"
                label="姓名"
                width="200">
        </el-table-column>
        <el-table-column
                prop="mobile"
                label="手机号"
                width="200">
        </el-table-column>
        <el-table-column
                prop="isdefault"
                label="是否默认"
                :formatter="isLine"
                width="200">
        </el-table-column>
        <el-table-column fixed="right" width="200" label="操作">
            <template slot-scope="scope">
                <el-button
                        size="mini"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button
                        size="mini"
                        type="danger"
                        @click="handleDelete(scope.$index,scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页功能-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5,10,15,20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--添加或者更新的form表单-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="用户id" :label-width="formLabelWidth">
                <el-input v-model="form.userid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth">
                <el-input v-model="form.mobile" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否默认" :label-width="formLabelWidth">
                <el-select v-model="form.isdefault" placeholder="是否添加为默认联系人" :label-width="formLabelWidth">
                    <el-option label="是" value="0"></el-option>
                    <el-option label="否" value="1"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>
    //js代码
    var vue = new Vue({
        el: "#app",
        data() {
            return {//存放使用的所有属性数据
                tableData: [],//方便编写表单校验
                currentPage: 1,//页码 第几页
                pageSize: 5,//每页显示数量
                total: 100,//分页总数量
                searchForm: {name: ''},//封装查询对象
                dialogFormVisible: false,//是否显示对话框
                title: '添加',//默认显示title
                form: {
                    userid: '',
                    name: '',
                    mobile: '',
                    isdefault: ''
                }, //form表单
                formLabelWidth: '120px',//labal宽度
            }
        },
        created: function () {
            this.loadData();
        },
        methods: {
            loadData() {
                var _this = this;
                axios.post('/tpt/queryPage',
                    {
                        "pageNo": this.currentPage, "pageSize": this.pageSize,
                        "data": {
                            "userid": this.searchForm.userid,
                            "name": this.searchForm.name,
                            "mobile": this.searchForm.mobile,
                            "isdefault": this.searchForm.isdefault
                        }
                    })
                    .then(function (response) {
                        console.log(response);
                        console.log(JSON.stringify(response));
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //每页数量发生变化
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                //调用加载数据方法
                this.loadData();
            },
            //页码（第几页）发生变化
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                //调用加载数据方法
                this.loadData();
            },
            //搜索方法
            search() {
                this.loadData();
            },
            //跳转更新
            handleEdit(index, row) {
                console.log(index, row);
                //让对话框显示
                this.dialogFormVisible = true;
                //让title值也发生变化
                this.title = '更新';
                //给表单赋值
                this.form = row;
            },


            //是否默认
            isLine(row, column, cellValue, index){
                var returnValue='';
                if (cellValue=='0'){
                    returnValue="是";
                }else{
                    returnValue="否";
                }
                return returnValue;
            },
            //跳转添加
            handleAdd() {
                //让对话框显示
                this.dialogFormVisible = true;
                //让title值也发生变化
                this.title = '添加';
                //让form清空
                this.form = {};
            },
            addOrUpdate() {
                var uri = "/tpt/tptAdd";
                if (this.title == '更新') {
                    uri = "/tpt/tptUpdate";
                }
                var _this = this;
                axios.post(uri, this.form)
                    .then(function (response) {
                        //前端控制台打印  response 是json对象
                        console.log(response);
                        console.log(JSON.stringify(response));
                        if (response.data.code == 200) {
                            //提示成功
                            _this.$message({
                                message: '恭喜你，这是一条成功消息',
                                type: 'success'
                            });
                            //关闭弹框
                            _this.dialogFormVisible = false;
                            //重新加载数据
                            _this.loadData();
                        } else {
                            _this.$message.error('错了哦，这是一条错误消息');
                        }
                    })
                    .catch(function (error) {//异常处理
                        console.log(error);
                    });
            },
            //处理删除
            handleDelete(index, row) {
                console.log(index, row);
                //定义变量
                var _this = this;

                this.$confirm('确定删除该数据?', '提示', {
                    type: 'warning'
                }).then(() => {
                    axios.post('/tpt/tptDelete/' + row.id).then(function (response) {
                        if (response.data.code == 200) {
                            //提示成功
                            _this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            //重新加载数据
                            _this.loadData();
                        } else {
                            _this.$message.error('删除失败');
                        }
                    }).catch((error) => {//异常处理
                        console.log("删除失败的原因：", error)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })
            },
        },
    })
</script>
</body>
</html>